package cwinsor.raking.year2014;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * The class is a table of Rakers
 *
 */
public class RakerTable {

	// list of rakers
	private Vector<PersonRaker> rakerList = new Vector<PersonRaker>();


	/*
	 * constructor
	 */
	public RakerTable() {
	}


	/*
	 * return the number of rows this table has
	 */
	public int getRowCount() {
		return rakerList.size();
	}


	/*
	 * return the object at [row,property]
	 */
	public Object getValueAt(int row, PROP_E propE) {
		PersonRaker raker = rakerList.get(row);
		PropertyBase propO = raker.getProp(propE);
		if (propO == null) {
			Msg.fatal(dS + ": got null when getting property " + propE.name());
		}
		return propO.getValue();
	}	


	/*
	 * read data in from .csv file
	 */
	public void readDataFromCsvFile(File csvFile) {
		if (dL > 0) {
			Msg.note(dS + ": START parsing input file " + csvFile.getName());
		}
		String line = "";
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(csvFile));

			while ((line = br.readLine()) != null) {
				if (dL > 0) {
					Msg.note(dS + ": parsing line of data --> " + line);
				}
				CsvParseRakers csvParser = new CsvParseRakers();
				if (csvParser.parse(line)) {
					PersonRaker raker = new PersonRaker();

					// assign values from the csv
					raker.fromCsvAllFields(csvParser);
					rakerList.add(raker);
				}
			}	
		} catch (FileNotFoundException e1) {
			Msg.note(dS + ": file not found exception ---");
			e1.printStackTrace();
		} catch (IOException e) {
			Msg.note(dS + ": problem when parsing the following line ---");
			Msg.note(line);
			Msg.note(dS + ": -----------------------------------------------");
			e.printStackTrace();
		}
		if (dL > 0) {
			Msg.note(dS + ": DONE parsing input file " + csvFile.getName());
		}
	}

	/*
	 * update rakers' schedule preferences (from .csv file)
	 */
	public void updateSchedulePreferencesFromCsvFile(File csvFile) {
		if (dL > 0) {
			Msg.note(dS + ": START parsing update file " + csvFile.getName());
		}
		String line = "";
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(csvFile));

			while ((line = br.readLine()) != null) {

				if (dL > 0) {
					Msg.note(dS + ": parsing line of data --> " + line);
				}
				CsvParseRakers csvParser = new CsvParseRakers();
				if (csvParser.parse(line)) {
					PersonRaker rakerUpdates = new PersonRaker();
					rakerUpdates.fromCsvNewSchedulePreferences(csvParser);

					// find the equivalent raker in the current data list
					Boolean found = false;
					for (PersonRaker rkr : rakerList) {
						if (rkr.sameName(rakerUpdates)) {
							found = true;
							// copy current preferences to last preferences
							rkr.agePreferences(csvParser);
						}
					}
					if (!found) {
						Msg.fatal(dS + ":when updating schedule preferences"
								+ " " + rakerUpdates.getProp(PROP_E.FIRST_NAME).toString()
								+ " " + rakerUpdates.getProp(PROP_E.LAST_NAME).toString()
								+ " is alltogether new (was not in the original list) !");
					}

				}
			}	
		} catch (FileNotFoundException e1) {
			Msg.note("--- file not found exception ---");
			e1.printStackTrace();
		} catch (IOException e) {
			Msg.note("--- problem when parsing the following line ---");
			Msg.note(line);
			Msg.note("-----------------------------------------------");
			e.printStackTrace();
		}
		if (dL > 0) {
			Msg.note(dS + ": DONE parsing update file " + csvFile.getName());
		}
	}



	/*
	 * write data to .csv file
	 */
	public void writeDataToCsvFile(File file) {

		try {
			if ( !file.exists() )
				file.createNewFile();
			FileWriter fw = new FileWriter(file);
			BufferedWriter writer = new BufferedWriter( fw );

			CsvParseRakers csvParser = new CsvParseRakers();
			for (PersonRaker person : rakerList) {
				String string = csvParser.generate(person);
				writer.write(string);
				writer.newLine();
			}
			writer.flush();
			writer.close();
			fw.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	// debug string, debug level
	private String  dS = this.getClass().getSimpleName();
	private Integer dL = 10;
}
